#include "gsmeg_link_info.h"
#include "gsmeg.h"

gsmeg_link_t *gsmeg_link_create(long long channel,int state){
    gsmeg_link_t *gsmeg_link =  (gsmeg_link_t *)calloc(1,sizeof(gsmeg_link_t));
    if(gsmeg_link){
        gsmeg_link->channel_ = channel;
        gsmeg_link->state_ = state;
    }else{
        Z_LOG_FATAL("gsmeg_link calloc fail");
    }
    return gsmeg_link;
}

void gsmeg_link_destroy(gsmeg_link_t *gsmeg_link){
    free(gsmeg_link);
}

int gsmeg_link_update_state(gsmeg_link_t *gsmeg_link,int state){
    _CHECK_NULL_RET_ERR_(gsmeg_link,-1)
    //todo --- whether to add state validation
    gsmeg_link->state_ = state;
    return 0;
}

int gsmeg_link_is_valid(gsmeg_link_t *gsmeg_link){
    _CHECK_NULL_RET_ERR_(gsmeg_link,FALSE)
    return gsmeg_link->state_ == GSMEG_LINK_LOGINED;
}

int gsmeg_link_send_msg(gsmeg_link_t *gsmeg_link, char *gsmeg_msg,int con_len,int cmd){
    _CHECK_NULL_RET_ERR_(gsmeg_link,-1)
    z_gsmeg_t *head = z_gsmeg_init_with_cfg(cmd,con_len,gsmeg_link->channel_);
    if(head){
        memcpy(head+1,gsmeg_msg,con_len);
        Z_LOG_DEBUG("send gsmeg to channel[%l]",gsmeg_link->channel_);
        print_gsmeg(head);
        g_api_lrm->push_(head);
        return 0;
    }else{
        Z_LOG_FATAL("init gsemg msg fail");
        return -1;
    }
}


